﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Riba.MVVMSL;

namespace MVVMtutorial
{
	public class MainPageViewModel: ViewModelBase
	{
		public RelayCommand CommandDialog { get; private set; }
		public RelayCommand CommandShowMessageBox { get; private set; }
		public RelayCommand CommandShowMessageWindow { get; private set; }
		public RelayCommand CommandOpenFileDialog { get; private set; }
		public RelayCommand CommandMouseEnter { get; private set; }

		private string _notificationFromDialog2;
		public string NotificationFromDialog2
		{
			get { return _notificationFromDialog2; }
			set { SetPropertyValue(ref _notificationFromDialog2, value, () => NotificationFromDialog2); }
		}

		public MainPageViewModel()
		{
			Notifier.Notify +=
				(sender, e) =>
					{
						if (e.Tag == "MyTag")
						{
							NotificationFromDialog2 = e.Data.ToString();

							if (e.Callback != null)
								e.Callback(NotificationFromDialog2 + " - Callback");
						}
					};

			CommandDialog = new RelayCommand
				(
					cmdPar =>
						{
							NavigateToViewEventArgs p;

							if (cmdPar.ToString() == "Dialog2")
								p = new NavigateToViewEventArgs(typeof(Dialog2), AnimationMode.Forward);
							else if (cmdPar.ToString() == "Dialog3")
								p = new NavigateToViewEventArgs(typeof(Dialog3), AnimationMode.Forward);
							else
								p = new NavigateToViewEventArgs(typeof(Dialog1), AnimationMode.Forward);

							RaiseNavigateToView(p);
						}
				) { IsEnabled = true };

			CommandShowMessageBox =
				new RelayCommand(OnCommandShowMessageBox) { IsEnabled = true };

			CommandShowMessageWindow =
				new RelayCommand
					(
						cmdPar =>
							{
								var buttons = new List<MessageWindowButton>();
								buttons.Add(new MessageWindowButton { Caption = "Ja", Tag = "yes" });
								buttons.Add(new MessageWindowButton { Caption = "Nein", Tag = "no" });
								var eventArgs = new ShowMessageWindowEventArgs(
									"Die Nachricht\n\nWeitere Zeile", "Titel", buttons, MessageWindow_Callback, null);
								RaiseShowMessageWindow(eventArgs);
							}
					) { IsEnabled = true };

			CommandOpenFileDialog = new RelayCommand
				(
					cmdPar =>
						{
							var openFileDialogEventArgs = new OpenFileDialogEventArgs("JPG Files (*.jpg;*.png)|*.jpg;*.jpg | All Files (*.*)|*.*", 1, false, OpenFileDialogCallback);
							RaiseOpenFileDialog(openFileDialogEventArgs);
						}
				);
			CommandOpenFileDialog.IsEnabled = true;

			CommandMouseEnter = new RelayCommand
				(
					cmdPar =>
						RaiseShowMsgBox(new ShowMsgBoxEventArgs(cmdPar.ToString(), "MouseEnter"))
				);
			CommandMouseEnter.IsEnabled = true;
		}

		private void OnCommandShowMessageBox(object commandParameter)
		{
			var args = new ShowMsgBoxEventArgs
				(
					"Mit OK bestätigen Sie Ihre Bestellung.", 
					"Bestellung", 
					MessageBoxButton.OKCancel,
					MsgCallback
				);

			RaiseShowMsgBox(args);
		}

		private void MsgCallback(MessageBoxResult result)
		{
			if (result == MessageBoxResult.OK)
				RaiseShowMsgBox(new ShowMsgBoxEventArgs("Bestellung aufgegeben", "Bestellung"));
		}

		private void MessageWindow_Callback(object tag)
		{
			object mode;
			if (tag.ToString() == "yes")
				mode = 1;
		}

		private void OpenFileDialogCallback(IEnumerable<FileInfo> files)
		{
			if (files.Count() == 0)
				return;

			var file = files.FirstOrDefault();
			RaiseShowMsgBox(new ShowMsgBoxEventArgs(file.Name, "Gewaaählte Datei"));
		}
	}
}